﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GE_Database_Manager
{
	public partial class ADRE_Form : Form
	{

        // data members
        private string pathCondition;
        private string pathKinematics;
        private string pathNameplate;
        private string pathWaveform;
        private string pathComments;
        private string pathConfigDB;
        public delegate void PassDatabase(object sender);
        public PassDatabase passDatabase;

		public ADRE_Form()
		{
            
			InitializeComponent();

            // machine condition textbox formating
            textBox_machineCondition.Font = new Font(textBox_machineCondition.Font, FontStyle.Italic);
            textBox_machineCondition.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineCondition.ForeColor = System.Drawing.Color.Gray;

            // machine kinematic textbox formating
            textBox_machineKinematics.Font = new Font(textBox_machineKinematics.Font, FontStyle.Italic);
            textBox_machineKinematics.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineKinematics.ForeColor = System.Drawing.Color.Gray;

            // machine nameplate textbox formating
            textBox_machineNameplate.Font = new Font(textBox_machineNameplate.Font, FontStyle.Italic);
            textBox_machineNameplate.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineNameplate.ForeColor = System.Drawing.Color.Gray;

            // machine waveform textbox formating
            textBox_machineWaveform.Font = new Font(textBox_machineWaveform.Font, FontStyle.Italic);
            textBox_machineWaveform.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineWaveform.ForeColor = System.Drawing.Color.Gray;

            // machine comments textbox formating
            textBox_machineComments.Font = new Font(textBox_machineComments.Font, FontStyle.Italic);
            textBox_machineComments.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineComments.ForeColor = System.Drawing.Color.Gray;

            // machine configDB textbox formating
            textBox_machineConfigDB.Font = new Font(textBox_machineConfigDB.Font, FontStyle.Italic);
            textBox_machineConfigDB.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_machineConfigDB.ForeColor = System.Drawing.Color.Gray;

            // initialize  paths
            pathCondition = null;
            pathKinematics = null;
            pathNameplate = null;
            pathWaveform = null;
            pathComments = null;
            pathConfigDB = null;
		}

        private void button_machineWaveform_Click(object sender, EventArgs e)
        {
            // create object to brows directories
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            // check that user hit ok, not x or cancel
            if (fbd.ShowDialog() == DialogResult.OK) {

                // add dir path to textbox
                textBox_machineWaveform.Text = fbd.SelectedPath;
                textBox_machineWaveform.Font = new Font(textBox_machineWaveform.Font, FontStyle.Regular);
                textBox_machineWaveform.ForeColor = System.Drawing.Color.Black;

                // add .dat files, from selected directory, to listbox
                listBox_machineWaveform.Items.Clear();
                string[] files = Directory.GetFiles(fbd.SelectedPath);
                foreach (string file in files)
                    if(Path.GetExtension(file) == ".dat")
                        listBox_machineWaveform.Items.Add(Path.GetFileName(file));
                if (listBox_machineWaveform.Items.Count == 0)
                    listBox_machineWaveform.Items.Add("ERROR: must select directory containing files of type .dat");
                else
                    pathWaveform = fbd.SelectedPath;
            }
        }

        private void button_machineKinematics_Click(object sender, EventArgs e)
        {
            // create object to brows files
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.xls*)|*.xls*";

            // add .xls file to textbox
            if (ofd.ShowDialog() == DialogResult.OK && Path.GetExtension(ofd.FileName).Contains(".xls")){
                textBox_machineKinematics.Text = Path.GetFileName(ofd.FileName);
                textBox_machineKinematics.Font = new Font(textBox_machineKinematics.Font, FontStyle.Regular);
                textBox_machineKinematics.ForeColor = System.Drawing.Color.Black;
                pathKinematics = ofd.FileName;
            }
            else
                textBox_machineKinematics.Text = "ERROR: must select kinematic file of type .xls";
        }

        private void button_machineNameplate_Click(object sender, EventArgs e)
        {
            // create object to brows .pdf files
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.pdf)|*.pdf";

            // add .pdf file to textbox
            if (ofd.ShowDialog() == DialogResult.OK && Path.GetExtension(ofd.FileName) == ".pdf")
            {
                textBox_machineNameplate.Text = Path.GetFileName(ofd.FileName);
                textBox_machineNameplate.Font = new Font(textBox_machineNameplate.Font, FontStyle.Regular);
                textBox_machineNameplate.ForeColor = System.Drawing.Color.Black;
                pathNameplate = ofd.FileName;
            }
            else
                textBox_machineNameplate.Text = "ERROR: must select nameplate file of type .pdf";
        }

        private void button_machineCondition_Click(object sender, EventArgs e)
        {
            // create object to brows .txt files only
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.txt)|*.txt";

            // add .txt file to textbox
            if (ofd.ShowDialog() == DialogResult.OK && Path.GetExtension(ofd.FileName) == ".txt")
            {
                textBox_machineCondition.Text = Path.GetFileName(ofd.FileName);
                textBox_machineCondition.Font = new Font(textBox_machineCondition.Font, FontStyle.Regular);
                textBox_machineCondition.ForeColor = System.Drawing.Color.Black;
                pathCondition = ofd.FileName;
            }
            else
                textBox_machineCondition.Text = "ERROR: must select malfunction file of type .txt";
        }

        private void button_machineComment_Click(object sender, EventArgs e)
        {
            // create object to brows .txt files only
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.txt)|*.txt";

            // add .txt file to textbox
            if (ofd.ShowDialog() == DialogResult.OK && Path.GetExtension(ofd.FileName) == ".txt")
            {
                textBox_machineComments.Text = Path.GetFileName(ofd.FileName);
                textBox_machineComments.Font = new Font(textBox_machineComments.Font, FontStyle.Regular);
                textBox_machineComments.ForeColor = System.Drawing.Color.Black;
                pathComments = ofd.FileName;
            }
            else
                textBox_machineCondition.Text = "ERROR: must select comment file of type .txt";
        }

        private void button_machineConfigDB_Click(object sender, EventArgs e)
        {
            // create object to brows .txt files only
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.xml)|*.xml";

            // add .txt file to textbox
            if (ofd.ShowDialog() == DialogResult.OK && Path.GetExtension(ofd.FileName) == ".xml")
            {
                textBox_machineConfigDB.Text = Path.GetFileName(ofd.FileName);
                textBox_machineConfigDB.Font = new Font(textBox_machineConfigDB.Font, FontStyle.Regular);
                textBox_machineConfigDB.ForeColor = System.Drawing.Color.Black;
                pathConfigDB = ofd.FileName;
            }
            else
                textBox_machineCondition.Text = "ERROR: must select config file of type .xml";
        }   

        private void button_ok_Click(object sender, EventArgs e)
        {
            if (pathCondition != null && pathKinematics != null && pathNameplate != null &&
                pathWaveform != null && pathComments != null && pathConfigDB != null)
            {
                // pass ADRE db to main form
                passDatabase(new ADRE(pathCondition, pathNameplate, pathKinematics, pathWaveform, pathComments, pathConfigDB));
                
                // close ADRE form
                this.Close();
            }
            else
                MessageBox.Show("ERROR: four files and a directory must be selected.");
        }   
	}
}
